package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.InputPwdActivity;
import flc.ast.activity.LocalVideoActivity;
import flc.ast.activity.ProActivity;
import flc.ast.activity.SetPwdActivity;
import flc.ast.adapter.HomeVideoAdapter;
import flc.ast.adapter.VideoAdapter;
import flc.ast.bean.MyVideoBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private HomeVideoAdapter homeVideoAdapter;
    private List<MyVideoBean> list = new ArrayList<>();//全部数据

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.ivMore.setOnClickListener(this);
        mDataBinding.llProConn.setOnClickListener(this);
        mDataBinding.ivEncipher.setOnClickListener(this);
        mDataBinding.tvNoPermission.setOnClickListener(this);
        mDataBinding.rvVideoList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        homeVideoAdapter=new HomeVideoAdapter();
        mDataBinding.rvVideoList.setAdapter(homeVideoAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivMore:
                startActivity(LocalVideoActivity.class);
                break;
            case R.id.llProConn://连接投屏
                startActivity(ProActivity.class);
                break;
            case R.id.ivEncipher://加密视屏
                //判断是否设置密码
                boolean isHave = SPUtil.getBoolean(mContext, Content.isHavePassword, false);
                if (isHave) {
                    startActivity(InputPwdActivity.class);
                } else {
                    startActivity(SetPwdActivity.class);
                }
                break;
            case R.id.tvNoPermission:
                getPermission();
                break;
        }
    }

    @Override
    protected void initData() {
        if (StkPermissionHelper.hasGrantMediaImgVideoPermission()){
            getData();
        }else {
            mDataBinding.tvNoPermission.setVisibility(View.VISIBLE);
            mDataBinding.rvVideoList.setVisibility(View.GONE);
            mDataBinding.ivLocalVideoNoData.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (Content.IsConnect){
            mDataBinding.tvDeviceName.setText(getString(R.string.pro_conn)+Content.ConnectName);
            mDataBinding.tvDeviceName.setTextColor(Color.parseColor("#FF3160FF"));
            mDataBinding.ivProConn.setImageResource(R.drawable.akaishouye);
        }else{
            mDataBinding.tvDeviceName.setText(getString(R.string.not_conn));
            mDataBinding.tvDeviceName.setTextColor(Color.parseColor("#FF7A7A7A"));
            mDataBinding.ivProConn.setImageResource(R.drawable.aguanshouy);
        }
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getString(R.string.get_storage_permission_home)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }

            @Override
            public void onDenied(boolean needGoSettings) {
                mDataBinding.rvVideoList.setVisibility(View.GONE);
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        list.clear();
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    for (SelectMediaEntity bean : selectMediaEntities) {
                        list.add(new MyVideoBean(bean.getMediaName(), bean.getPath(), bean.getUri(), bean.getDuration(), FileUtils.getFileLastModified(bean.getPath()), bean.getSize(), false));
                    }
                    if (list.size()>3){
                        list=list.subList(0,3);
                    }
                    homeVideoAdapter.setList(list);
                    mDataBinding.tvNoPermission.setVisibility(View.GONE);
                    mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.ivLocalVideoNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.tvNoPermission.setVisibility(View.GONE);
                    mDataBinding.rvVideoList.setVisibility(View.GONE);
                    mDataBinding.ivLocalVideoNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}